/**
 * 
 */
package com.itinsys.enma.manager.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialException;

import org.apache.log4j.Logger;
import org.hibernate.lob.SerializableBlob;

import com.itinsys.enma.domain.Environment;
import com.itinsys.enma.domain.Resource;
import com.itinsys.enma.domain.dto.EnvironmentDTO;
import com.itinsys.enma.manager.EnvironmentDTOManager;
import com.thoughtworks.xstream.core.util.Base64Encoder;
/**
 * @author pferraz
 *
 */
public class EnvironmentDTOManagerImpl implements EnvironmentDTOManager {

	private static Logger logger = Logger.getLogger("service");
	
	/**
	 * 
	 */
	public EnvironmentDTOManagerImpl() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<EnvironmentDTO> listToDTO(List<Environment> environments) {
		List<EnvironmentDTO> environmentDTOs=new ArrayList<EnvironmentDTO>();
		for (Environment e:environments)
			environmentDTOs.add(envToDTO(e));
		
		return environmentDTOs;
	}


	@Override
	public List<Environment> dtoToList(List<EnvironmentDTO> environmentDTOs) {
		List<Environment> environments=new ArrayList<Environment>();
		for (EnvironmentDTO eDTO:environmentDTOs){
			byte[] bytes=eDTO.getLocation().getBytes();
			try {
				environments.add(new Environment(eDTO.getId(), eDTO.getDescription(), eDTO.getBuilding(), eDTO.getCapacity(), new SerializableBlob(new SerialBlob(bytes))));
			} catch (SerialException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return environments;
	}

	@Override
	public Map<String, String> envToREnvDTO(Environment e) {
		Map<String,String> rEnvDTO=new HashMap<String, String>();
		rEnvDTO.put("description", e.getDescription());
		String resources="";
		for(Resource r:e.getResources()){
			resources+=r.getDescription()+",";
		}
		if(!e.getResources().isEmpty())
			resources=resources.substring(0, resources.length()-1);
		rEnvDTO.put("resources", resources);
		return rEnvDTO;
	}

	@Override
	public EnvironmentDTO envToDTO(Environment env) {
		byte[] imageBytes = null;

        String imageString="";
		if(env.getLocation()!=null){
			try {
				imageBytes = env.getLocation().getBytes(1, (int)env.getLocation().length());
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
	
	        Base64Encoder encoder = new Base64Encoder();
	        String i = encoder.encode(imageBytes);
	        
	        String ext=i.substring(10,14);
			if(ext.equalsIgnoreCase("jpeg"))
				imageString=i.substring(0, 4)+":"+i.substring(4, 14)+";"+i.substring(14, 20)+","+i.substring(20,i.length()-2)+"/Z";	
	        else
	        	imageString=i.substring(0, 4)+":"+i.substring(4, 13)+";"+i.substring(13, 19)+","+i.substring(19,i.length()-2)+"C";
		}
        
		return new EnvironmentDTO(env.getId(), env.getDescription(), env.getBuilding(), env.getCapacity(), imageString, env.getResources(), env.isActive());
	}




}
